﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_ReceiveTypes : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MAS_ReceiveTypes type = new B_FIM_MAS_ReceiveTypes();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                UpdateUI1("ViewAll");
            }
        }

        protected void LoadRadGridReceiveTypes()
        {
            RadGridReceiveTypes.DataSource = type.GetFull(null);
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetFull(true);
            ddlBgmSourceSeq.DataBind();
        }

        protected IList<M_BGM_MAS_Source> LoadDDLFilterBgmSourceSeq()
        {
            return source.GetList();
        }

        protected void LoadDDLBankAccSeq()
        {
            ddlBankAccSeq.DataSource = acc.GetList(null, ddlBgmSourceSeq.SelectedValue, null, null);
            ddlBankAccSeq.DataBind();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGridReceiveTypes.Visible = true;
                    PanelEditReceiveTypes.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGridReceiveTypes.Visible = false;
                    PanelEditReceiveTypes.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGridReceiveTypes.Visible = false;
                    PanelEditReceiveTypes.Visible = true;
                    break;
            }
        }

        protected void ClearControl1()
        {
            lblReceiveTypeSeq.Text = "";
            txtReceiveTypeName.Text = "";
            txtRemarks.Text = "";
            ddlBgmSourceSeq.SelectedIndex = 0;
            rblInvConnectType.SelectedIndex = 0;
        }

        //============================ control events ==============================

        protected void RadGridReceiveTypes_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strReceiveTypeSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_type_seq"].Text;
                Session["ReceiveTypeSeq"] = strReceiveTypeSeq;
            }
        }

        protected void RadGridReceiveTypes_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGridReceiveTypes();
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
            LoadDDLBgmSourceSeq();
            LoadDDLBankAccSeq();

            rblIsConnect_SelectedIndexChanged(null, null);
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGridReceiveTypes.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strReceiveTypeSeq = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["receive_type_seq"].Text;
                string strReceiveTypeName = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["receive_type_name"].Text;
                string strBgmSourceSeq = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["bgm_source_seq"].Text;
                string strBankAccSeq = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["bank_acc_seq"].Text;
                string strInvConnectType = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["inv_connect_type"].Text;
                string strRemarks = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["remarks"].Text;

                LoadDDLBgmSourceSeq();

                lblReceiveTypeSeq.Text = main.WebString(strReceiveTypeSeq);
                txtReceiveTypeName.Text = main.WebString(strReceiveTypeName);
                ddlBgmSourceSeq.SelectedValue = main.WebString(strBgmSourceSeq);
                LoadDDLBankAccSeq();
                ddlBankAccSeq.SelectedValue = main.WebString(strBankAccSeq);
                txtRemarks.Text = main.WebString(strRemarks);

                strInvConnectType = main.WebString(strInvConnectType);
                if (strInvConnectType == "0")
                {
                    rblIsConnect.SelectedValue = "0";
                    rblInvConnectType.Visible = false;
                }
                else
                {
                    rblIsConnect.SelectedValue = "1";
                    rblInvConnectType.Visible = true;
                    rblInvConnectType.SelectedValue = strInvConnectType;
                }
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGridReceiveTypes.SelectedItems.Count > 0)
            {
                M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strReceiveTypeSeq = RadGridReceiveTypes.Items[RadGridReceiveTypes.SelectedItems[0].ItemIndex]["receive_type_seq"].Text;

                info.receive_type_seq = Convert.ToInt32(strReceiveTypeSeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = dtNow;

                type.Delete(info);

                LoadRadGridReceiveTypes();
                RadGridReceiveTypes.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();
            //string strReceiveTypeSeq = lblReceiveTypeSeq.Text;
            string strReceiveTypeName = txtReceiveTypeName.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strBankAccSeq = ddlBankAccSeq.SelectedValue;
            string strInvConnectType = (rblIsConnect.SelectedValue == "0" ? "0" : rblInvConnectType.SelectedValue);
            bool bTempIncome = chkTempIncome.Checked;
            string strRemarks = txtRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.receive_type_name = strReceiveTypeName;
            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.inv_connect_type = strInvConnectType;
            info.temp_income = bTempIncome;
            info.remarks = strRemarks;
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            type.Insert(info);

            LoadRadGridReceiveTypes();
            RadGridReceiveTypes.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();
            string strReceiveTypeSeq = lblReceiveTypeSeq.Text;
            string strReceiveTypeName = txtReceiveTypeName.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strBankAccSeq = ddlBankAccSeq.SelectedValue;
            string strInvConnectType = (rblIsConnect.SelectedValue == "0" ? "0" : rblInvConnectType.SelectedValue);
            bool bTempIncome = chkTempIncome.Checked;
            string strRemarks = txtRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.receive_type_seq = Convert.ToInt32(strReceiveTypeSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.receive_type_name = strReceiveTypeName;
            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.inv_connect_type = strInvConnectType;
            info.temp_income = bTempIncome;
            info.remarks = strRemarks;
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            type.Update(info);

            LoadRadGridReceiveTypes();
            RadGridReceiveTypes.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        protected void rblIsConnect_SelectedIndexChanged(object sender, EventArgs e)
        {
            PanelInventory.Visible = rblIsConnect.SelectedIndex == 1;
        }

        protected void ddlBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLBankAccSeq();
        }
        
        //==========================================================================
    }
}
